package com.vpx.server.service.system.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.bean.entity.CProjectFlowInstance;
import com.vpx.server.bean.entity.ProjectFlow;
import com.vpx.server.bean.order.base.BaseDelOrder;
import com.vpx.server.bean.order.base.BaseIdStrOrder;
import com.vpx.server.bean.order.system.ProjectFlowOrder;
import com.vpx.server.bean.order.system.ProjectFlowPageOrder;
import com.vpx.server.bean.result.system.ProjectFlowPageResult;
import com.vpx.server.bean.result.system.ProjectFlowResult;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.mapper.system.ProjectFlowMapper;
import com.vpx.server.service.system.CProjectFlowInstanceService;
import com.vpx.server.service.system.ProjectFlowService;
import com.vpx.server.service.system.SqlExtService;
import com.vpx.server.service.system.SqlService;
import com.vpx.server.utils.UserLoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author PC
 * @Date 2023/11/18 16:31
 **/
@Service
public class ProjectFlowServiceImpl extends ServiceImpl<ProjectFlowMapper, ProjectFlow> implements ProjectFlowService {
    @Resource
    private SqlExtService sqlExtService;

    @Resource
    private SqlService sqlService;
    @Override
    public ResultMsg saveProjectFlow(ProjectFlowOrder order) {
        //c_project_flow 表保存数据
        ProjectFlow entity = new ProjectFlow();
        BeanUtil.copyProperties(order,entity);
        entity.setCreateUser(UserLoginUtils.getCurrentUser() == null ? "admin" : UserLoginUtils.getCurrentUser().getId().toString());
        entity.setCreateTime(DateUtils.getDateTimeStr());
        baseMapper.insert(entity);

        //c_project_flow_instance 插入数据
        Map<String,Object> paramFlow = new HashMap<>();
        paramFlow.put("id",entity.getId());
        paramFlow.put("instance_name",entity.getFlowName());
        paramFlow.put("instance_content","");
        paramFlow.put("create_time",entity.getCreateTime());
        paramFlow.put("bus_type","3");//流程
        paramFlow.put("project_id",order.getProjectId());
        sqlService.execute("10",paramFlow);

        //插入属性数据 c_project_attr
        List<JSONObject> list = new ArrayList<>();
        JSONObject obj6 = JSONUtil.createObj();
        obj6.putByPath("id", entity.getId());
        obj6.putByPath("name", entity.getFlowName());
        obj6.putByPath("create_time", entity.getCreateTime());
        obj6.putByPath("project_id", entity.getProjectId());
        obj6.putByPath("config_type", "6");
        list.add(obj6);
        Map<String,Object> param = new HashMap<>();
        param.put("list",list);
        this.sqlService.execute("2",param);

        //插入c_base_info 流程数据
        //批量保存数据
        List<JSONObject> baseInfoList = new ArrayList<>();
        JSONObject baseInfo = JSONUtil.createObj();
        baseInfo.putByPath("id", entity.getId());
        baseInfo.putByPath("parent_id", "");
        baseInfo.putByPath("type", "3");//3、流程
        baseInfo.putByPath("node_type", "4"); //4 流程
        baseInfo.putByPath("name", entity.getFlowName());
        baseInfo.putByPath("parent_all_id", entity.getId());
        baseInfo.putByPath("num", IdUtil.getSnowflakeNextIdStr());
        baseInfo.putByPath("notes", "false");
        baseInfo.putByPath("project_id", entity.getProjectId());
        baseInfo.putByPath("create_user", UserLoginUtils.getCurrentUser() == null ? "admin" : UserLoginUtils.getCurrentUser().getId().toString());
        baseInfo.putByPath("create_time", DateUtils.getDateTimeStr());
        baseInfoList.add(baseInfo);
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("list", baseInfoList);
        this.sqlService.execute("9", paramMap);

        return ResultMsg.success(obj6);
    }

    @Override
    public ResultMsg deleteProjectFlow(BaseDelOrder order) {
        baseMapper.deleteById(order.getId());
        String delBaseInfo="delete from c_base_info where parent_all_id like '%"+order.getId()+"%'";
        this.sqlExtService.execute(delBaseInfo);
        String delProjectAttr="delete from c_project_attr where id = '"+order.getId()+"'";
        this.sqlExtService.execute(delProjectAttr);
        String delProjectFlowInstance="delete from c_project_flow_instance where id='"+order.getId()+"'";
        this.sqlExtService.execute(delProjectFlowInstance);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg updateProjectFlowById(ProjectFlowOrder order) {
        if (StrUtil.isEmpty(order.getId())){
            return ResultMsg.fail("数据id不能为空");
        }
        ProjectFlow project = baseMapper.selectById(order.getId());
        if (project == null){
            return ResultMsg.fail("数据不存在，请刷新重试");
        }
        BeanUtil.copyProperties(order,project);
        updateById(project);
        return ResultMsg.success();
    }

    @Override
    public ResultMsg<ProjectFlowResult> selectProjecFlowById(BaseIdStrOrder order) {
        if (StrUtil.isEmpty(order.getId())){
            return ResultMsg.fail("数据id不能为空");
        }
        ProjectFlow project = baseMapper.selectById(order.getId());
        if (project == null){
            return ResultMsg.fail("数据不存在，请刷新重试");
        }
        ProjectFlowResult result = new ProjectFlowResult();
        BeanUtil.copyProperties(project,result);
        return ResultMsg.success(result);
    }

    @Override
    public ResultMsg<ProjectFlowPageResult> pageQueryProjectFlow(ProjectFlowPageOrder order) {
        PageHelper.startPage(order.getPageNum(), order.getPageSize());
        QueryWrapper<ProjectFlow> wrapper = new QueryWrapper();
        wrapper.like(StrUtil.isNotBlank(order.getFlowName()),"flow_name",order.getFlowName());
        wrapper.eq(StrUtil.isNotBlank(order.getProjectId()),"project_id",order.getProjectId());
        List<ProjectFlow> projects = this.list(wrapper);
        PageInfo<ProjectFlow> pageInfo = new PageInfo<>(projects);
        PageInfo<ProjectFlowResult> pageInfo2 = new PageInfo<>();
        BeanUtil.copyProperties(pageInfo,pageInfo2);
        ProjectFlowPageResult result = new ProjectFlowPageResult();
        result.setPageInfo(pageInfo2);
        return ResultMsg.success(result);
    }
}
